package com.project.webpage.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.project.webpage.dto.SearchByRqRangeParam;
import com.project.webpage.entity.WorkCalendar;
import com.project.webpage.mapper.WorkCalendarMapper;
import com.project.webpage.service.WorkCalendarService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class WorkCalendarServiceImpl implements WorkCalendarService {

    @Resource
    private WorkCalendarMapper workCalendarMapper;

    @Override
    @DS("master")
    public List<WorkCalendar> getListForPage(SearchByRqRangeParam param) {
        Example example = new Example(WorkCalendar.class);
        Example.Criteria c = example.createCriteria();
        c.andGreaterThanOrEqualTo("calendar_date", param.getStartDate());
        c.andLessThanOrEqualTo("calendar_date", param.getEndDate());
        example.orderBy("calendar_date").asc();
        return workCalendarMapper.selectByExample(example);
    }

    @Override
    @DS("master")
    public List<WorkCalendar> getByRq(String rq) {
        Example example = new Example(WorkCalendar.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("calendar_date", rq);
        return workCalendarMapper.selectByExample(example);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void update(WorkCalendar entity, String dateBefore) {
        Example example = new Example(WorkCalendar.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("calendar_date", dateBefore);
        workCalendarMapper.updateByExampleSelective(entity, example);
    }
}
